package com.cynoware.busappmas.contentprovider;


import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

import com.cynoware.busappmas.model.VideoInfo;

public class DbVideoList{
	private static final String TAG = "DbVideoList";
	private ContentResolver mContentResolver;
	
	public DbVideoList(ContentResolver mContentResolver){
		this.mContentResolver = mContentResolver;
	}
	
	public VideoInfo popVideo(){
		VideoInfo vp = null;
		String[] projection ={DbHelper.COLUME_VIDEOLIST_ID, DbHelper.COLUME_VIDEOLIST_NAME,DbHelper.COLUME_VIDEOLIST_POSITION};
		Cursor cursor = mContentResolver.query(DbContentProvider.CONTENT_URI_VIDEOLIST, projection, null, null, "ID DESC");
		if (cursor.moveToNext()) {
	    	vp=new VideoInfo();
			vp.setId(cursor.getInt(0));
			vp.setName(cursor.getString(1));
			vp.setPosition(cursor.getInt(2));
			Log.d(TAG,"pop video "+cursor.getString(1)+",position "+cursor.getInt(2));
			mContentResolver.delete(DbContentProvider.CONTENT_URI_VIDEOLIST, DbHelper.COLUME_VIDEOLIST_ID+"=?",new String[]{String.valueOf(vp.getId())}); 
		}
	    cursor.close();
	    
	    return vp;
	}
	
	public void pushVideo(String name,int position){
		ContentValues cv = new ContentValues();
		cv.put(DbHelper.COLUME_VIDEOLIST_NAME, name);
		cv.put(DbHelper.COLUME_VIDEOLIST_POSITION, position);
		mContentResolver.insert(DbContentProvider.CONTENT_URI_VIDEOLIST,  cv);
		Log.d(TAG,"push video "+name+",position "+position);	
	}
	
	public void deleteAllPopInfo(){
		mContentResolver.delete(DbContentProvider.CONTENT_URI_VIDEOLIST, null, null);
	}
}